package kg.aibek.projecteuler.p10;

import java.util.ArrayList;
import java.util.List;

import kg.aibek.preuler1.Util;

public class Problem43 {
	int[] prime = { 2, 3, 5, 7, 11, 13, 17 };

	public void solve() {
		List<Integer> nList = new ArrayList<Integer>();
		for (int i = 0; i < 10; i++) {
			nList.add(i);
		}

		List<String> list = Util.getPermutation(nList);

		long count = 0;
		for (String str : list) {
			if (hasProperty(str)) {
				count += Long.parseLong(str);
				System.out.println(str);
			}
		}

		System.out.println(count);
	}

	private boolean hasProperty(String str) {
		for (int i = 0; i < 7; i++) {
			int b = Integer.parseInt(str.substring(i + 1, i + 4));
			if (b % prime[i] != 0)
				return false;
		}

		return true;
	}

	public static void main(String[] args) {
		Problem43 p = new Problem43();
		p.solve();
	}
}
